package com.parenting.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.parenting.entities.ExamineDO;
import com.parenting.entities.OrdersDO;
import com.parenting.feign.controller.IFeignAppletActivityController;
import com.parenting.mapper.ExamineMapper;
import com.parenting.model.ActivityDTO;
import com.parenting.model.UserDTO;
import com.parenting.response.RefuseRefundResponse;
import com.parenting.response.applet.ApprovalList;
import com.parenting.response.applet.RefundDetails;
import com.parenting.service.ExamineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parenting.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-10-13
 */
@Service
public class ExamineServiceImpl extends ServiceImpl<ExamineMapper, ExamineDO> implements ExamineService {

    @Override
    public List<ApprovalList> getApprovalList(String shopId) {
        List<ApprovalList> approvalLists=new ArrayList<>();
        LambdaQueryWrapper<ExamineDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ExamineDO::getShopId, shopId);
        wrapper.eq(ExamineDO::getOrderStatus,"1");
        wrapper.eq(ExamineDO::getType,1);
        List<ExamineDO> examineDOS = baseMapper.selectList(wrapper);
        if (null != examineDOS&&examineDOS.size()>0){
            for (ExamineDO e:examineDOS) {
                approvalLists.add(BeanUtil.copyProperties(e,ApprovalList.class));
            }
        }
        return approvalLists;
    }

    @Override
    public List<ApprovalList> getAlreadyApprovalList(String shopId) {
        List<ApprovalList> approvalLists=new ArrayList<>();
        LambdaQueryWrapper<ExamineDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ExamineDO::getShopId, shopId);
        wrapper.eq(ExamineDO::getOrderStatus,"2").or().eq(ExamineDO::getOrderStatus,"3");
        wrapper.eq(ExamineDO::getType,2);
        List<ExamineDO> examineDOS = baseMapper.selectList(wrapper);
        if (null != examineDOS&&examineDOS.size()>0){
            for (ExamineDO e:examineDOS) {
                approvalLists.add(BeanUtil.copyProperties(e,ApprovalList.class));
            }
        }
        return approvalLists;
    }

    @Autowired
    private OrdersService ordersService;

    @Override
    @Transactional
    public void ByRefund(UserDTO userInfo, String orderId) {
        ExamineDO examineDo = getExamineDo(userInfo, orderId);
        baseMapper.insert(examineDo);
        ordersService.ByRefund(orderId);
    }

    @Override
    @Transactional
    public void RefuseRefund(UserDTO userInfo, RefuseRefundResponse refuseRefundResponse) {
        ExamineDO examineDo = getExamineDo(userInfo, refuseRefundResponse.getOrderId());
        baseMapper.insert(examineDo);
        ordersService.RefuseRefund(refuseRefundResponse);
    }
    @Autowired
    private IFeignAppletActivityController feignAppletActivityController;
    @Override
    public RefundDetails refundDetails(String orderId) {
        //查询退款人
        LambdaQueryWrapper <ExamineDO> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ExamineDO::getOrdersId,orderId);
        wrapper.eq(ExamineDO::getType,1);
        RefundDetails refundDetails=null;
        //得到申请人记录
        ExamineDO examineDO = baseMapper.selectOne(wrapper);
        if (null!=examineDO){
             refundDetails = BeanUtil.copyProperties(examineDO, RefundDetails.class);
            OrdersDO byId = ordersService.getById(orderId);
            if (null!=examineDO){
                refundDetails.setPaid(byId.getPaid());
                refundDetails.setItemConcessional(byId.getItemConcessional()*byId.getItemNumber());
                refundDetails.setOriginalPrice(byId.getOriginalPrice());
                refundDetails.setRefundAmount(byId.getRefundAmount());
                refundDetails.setRefundReason(byId.getRefundReason());
                ActivityDTO activityById = feignAppletActivityController.getActivityById(byId.getActivityId());
                if (null!=activityById ){
                    refundDetails.setActivityName(activityById.getActivityName());
                    refundDetails.setActivityStartTime(activityById.getActivityStartTime());
                    refundDetails.setActivityEndTime(activityById.getActivityEndTime());
                }

            }
        }
      return refundDetails;
    }


    private  ExamineDO getExamineDo(UserDTO userInfo,String orderId){
        ExamineDO  examineDO=new ExamineDO();
        examineDO.setOrdersId(orderId);
        examineDO.setOrderStatus("2");
        examineDO.setUsername(userInfo.getFullName());
        examineDO.setUserId(userInfo.getId());
        examineDO.setShopId(userInfo.getShopId());
        examineDO.setType(2);
        examineDO.setCreationTime(new Date());
        return examineDO;
    }

}
